using System;
using System.Web;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;

namespace ExaminationSys
{
    public class BasePage : System.Web.UI.Page
    {
        private static readonly StudentService Proxy = ObjectFactory<StudentService>.Create();

        private static readonly StudentLevelService ProxyStudentLevel = ObjectFactory<StudentLevelService>.Create();
        private static readonly SectionInfoService ProxySectionInfoService = ObjectFactory<SectionInfoService>.Create();

        public bool CheckSectionId(string sectionId)
        {
            var id = Guid.Empty;
            if (Guid.TryParse(sectionId, out id))
            {
                var item = ProxySectionInfoService.Get(id);
                return item != null;
            }
            return false;
        }
        protected override void OnInit(EventArgs e)
        {
            if (System.Web.HttpContext.Current != null)
            {
                base.OnInit(e);
            }
        }

        public int GetCurrentStudentLevel()
        {
            ResetSession();
            var loginInfo = Session["StudentInfo"] as Student;
            if (loginInfo != null)
            {
                var levelEntity = ProxyStudentLevel.GetTestLevelByUserId(loginInfo.UserID);
                return levelEntity == null ? 1 : levelEntity.TestLevel;

            }
            else
            {
                Response.Redirect("Index.aspx");
                return 1;
            }
        }

        public void ResetSession()
        {
            var loginInfo = Session["StudentInfo"];
            if (loginInfo == null)
            {
                HttpCookie cookie = Request.Cookies["SJEDU_UID"];
                if (cookie != null)
                {
                    var uid = cookie["UID"];
                    var pwd = cookie["PWD"];

                    var student = Proxy.Login(uid, pwd);
                    if (student == null)
                    {
                        Response.Redirect("Index.aspx");
                    }
                    else
                    {
                        Session["StudentInfo"] = student;
                    }
                }
                else
                {
                    Response.Redirect("Index.aspx");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var loginInfo = Session["StudentInfo"];
            if (loginInfo == null)
            {
                HttpCookie cookie = Request.Cookies["SJEDU_UID"];
                if (cookie != null)
                {
                    var uid = cookie["UID"];
                    var pwd = cookie["PWD"];

                    var student = Proxy.Login(uid, pwd);
                    if (student== null)
                    {
                        Response.Redirect("Index.aspx");
                    }
                    else
                    {
                        Session["StudentInfo"] = student;
                    }
                }
                else
                {
                    Response.Redirect("Index.aspx");
                }
            }
        }

    }
}